<template>
  <div class="users">
    <div class="breadcrumb-wrap">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="inpt-wrap">
      <el-input
        placeholder="请输入内容"
        style="width:350px;margin-right:15px;"
        clearable
        v-model="usersOptions.query"
        @keyup.enter.native="search"
      >
        <el-button slot="append" icon="el-icon-search" @click.native="search"></el-button>
      </el-input>
      <el-button type="primary" @click="addUser">添加用户</el-button>
    </div>

    <div class="table-wrap">
      <el-table :data="userLists" border style="width: 100%;margin-bottom:20px;" max-height="400">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="username" label="姓名" width="180"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column label="创建日期">
          <template slot-scope="scope">
            <span>{{scope.row.create_time | dealDate}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="mg_state" label="用户状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.mg_state" @change="_changeUserStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="_editUser(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="_deleteUser(scope.row)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-check"
              circle
              size="mini"
              @click="_changeRole(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="usersOptions.pagenum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="usersOptions.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <el-dialog title="添加用户" :visible.sync="dialogVisible" width="600px">
      <el-form
        label-position="right"
        ref="validateForm"
        :rules="test"
        label-width="80px"
        :model="userForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userForm.username"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
          :rules="[
      { required: true, message: '请输入密码', trigger: 'blur' }]"
        >
          <el-input v-model="userForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱">
          <el-input v-model="userForm.email"></el-input>
        </el-form-item>
        <el-form-item prop="mobile" label="电话">
          <el-input v-model="userForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doAddUser">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="编辑用户" :visible.sync="editDialogStatus" width="600px">
      <el-form label-position="right" label-width="80px" :model="editUserForm">
        <el-form-item label="用户名">
          <el-input disabled v-model="editUserForm.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="editUserForm.email"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="editUserForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogStatus = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改角色弹窗 -->
    <el-dialog title="修改角色" :visible.sync="roleDialogStatus" width="600px">
      <el-form label-position="right" label-width="80px" :model="roleForm">
        <el-form-item label="用户名">
          <el-input disabled v-model="roleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="roleForm.rid">
            <el-option disabled label="请选择" :value="-1"></el-option>
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="roleDialogStatus = false">取 消</el-button>
        <el-button type="primary" @click="_editRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUsers,
  addUsers,
  changeUserStatus,
  editUsers,
  deleteUsers,
  getRolesList,
  getUserInfoById,
  editRole
} from "@/http/api";
import moment from "moment";
export default {
  data() {
    function validates(rule, value, callback) {
      if (value === "admin") {
        callback(new Error("用户名admin"));
      } else {
        callback();
      }
    }
    return {
      usersOptions: {
        query: "",
        pagenum: 1,
        pagesize: 8
      },
      userLists: [],
      totalSize: 0,
      dialogVisible: false,
      userForm: {
        username: "",
        password: "",
        email: "",
        mobile: ""
      },
      test: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            validator: validates
          }
        ]
      },
      validateForm: {
        username: "",
        password: "",
        email: "",
        mobile: ""
      },
      editDialogStatus: false,
      editUserForm: {
        username: "",
        email: "",
        mobile: "",
        id: ""
      },
      roleDialogStatus: false,
      roleForm: {
        username: "",
        id: "",
        rid: "",
        roleName: ""
      },
      roleList: [
        {
          id: 0,
          roleName: ""
        }
      ]
    };
  },
  methods: {
    go() {
      console.log(123)
    },
    //分页器页数大小发生变化时触发
    handleSizeChange(val) {
      // this.usersOptions.pagesize = val
      // getUsers(this.usersOptions).then(res => {
      //   this.userLists = res.data.users
      //   this.totalSize = res.data.total
      // })
      this._getUsers("pagesize", val);
    },
    //分页器当前页码发生变化时触发
    handleCurrentChange(val) {
      // this.usersOptions.pagenum = val
      // getUsers(this.usersOptions).then(res => {
      //   this.userLists = res.data.users
      //   this.totalSize = res.data.total
      // })
      this._getUsers("pagenum", val);
    },
    //获取用户列表
    _getUsers(params, val) {
      if (params) {
        this.usersOptions[params] = val;
      }
      getUsers(this.usersOptions).then(res => {
        this.userLists = res.data.users;
        this.totalSize = res.data.total;
      });
    },
    search() {
      this._getUsers();
    },
    handleClose() {},
    //添加用户弹窗展示
    addUser() {
      this.dialogVisible = true;
    },
    //添加用户
    doAddUser() {
      addUsers(this.userForm).then(res => {
        this.$message({
          type: "success",
          message: "添加成功"
        });
        this.dialogVisible = false;
        this.$refs.validateForm.resetFields();
        this._getUsers();
      });
    },
    clearData() {
      for (item in this.userForm) {
        this.userForm[item] = "";
      }
    },
    _changeUserStatus(val) {
      changeUserStatus(val).then(res => {
        this.$message({
          type: "success",
          message: "修改用户状态成功"
        });
        this._getUsers();
      });
    },
    _editUser(val) {
      this.editDialogStatus = true;
      this.editUserForm.username = val.username;
      this.editUserForm.email = val.email;
      this.editUserForm.mobile = val.mobile;
      this.editUserForm.id = val.id;
    },
    editUser() {
      editUsers(this.editUserForm).then(res => {
        this.$message({
          tyep: "success",
          message: "修改用户信息成功"
        });
        this._getUsers();
        this.editDialogStatus = false;
      });
    },
    _deleteUser(val) {
      this.$confirm("是否删除该用户?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteUsers(val).then(res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this._getUsers();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //获取角色列表
    _getRolesList() {
      getRolesList().then(res => {
        this.roleList = res.data;
      });
    },
    //回显角色
    _changeRole(val) {
      this.roleDialogStatus = true;
      this._getRolesList();
      getUserInfoById(val).then(res => {
        this.roleForm.rid = res.data.rid;
        this.roleForm.username = res.data.username;
        this.roleForm.id = res.data.id;
      });
    },
    //确认修改用户角色
    _editRole() {
      editRole(this.roleForm).then(res => {
        this.$message({
          type: "success",
          message: "修改权限成功"
        });
        this.roleDialogStatus = false;
        this._getUsers();
      });
    }
  },
  created() {
    // getUsers(this.usersOptions).then(res => {
    //   this.userLists = res.data.users
    //   this.totalSize = res.data.total
    // })
    this._getUsers();
  },
  filters: {
    dealDate(val) {
      return moment(val).format("YYYY-MM-DD");
    }
  }
};
</script>
<style scoped lang="stylus">
.users
  padding 15px
  background #fff
  border-radius 10px
  .breadcrumb-wrap
    margin-bottom 20px
  .inpt-wrap
    display flex
    justify-content flex-start
    margin-bottom 20px
</style>